package ru.nsu.fit.iconveyor.example.math.node;

import ru.nsu.fit.iconveyor.conveyor.Conveyor;
import ru.nsu.fit.iconveyor.conveyor.storage.exporter.Exporter;
import ru.nsu.fit.iconveyor.conveyor.storage.importer.Importer;
import ru.nsu.fit.iconveyor.engine.ConveyorExecutionObserver;
import ru.nsu.fit.iconveyor.engine.Engine;
import ru.nsu.fit.iconveyor.engine.smart.SmartEngine;
import ru.nsu.fit.iconveyor.log.console.ConsoleLoggingConveyorExecutionObserver;
import ru.nsu.fit.iconveyor.log.console.ConsoleLoggingEngineStateObserver;

import java.util.Collection;

public class MathTest {

    public static void main(String [] args) {
        Conveyor conveyor = new Conveyor();

        StdInNumberInputNode inputA = new StdInNumberInputNode("A");
        inputA.setParameter(StdInNumberInputNode.INPUT_NUMBER, "5");
        StdInNumberInputNode inputB = new StdInNumberInputNode("B");
        inputB.setParameter(StdInNumberInputNode.INPUT_NUMBER, "10");

        AdditionNode addition = new AdditionNode("+");

        StdOutNumberOutputNode outputC = new StdOutNumberOutputNode("C");

        int id = 0;
        {
            String channelId  = Integer.toString(id++);
            inputA.getOutputNodeConnectionByName(StdInNumberInputNode.OUTPUT_RESULT).setChannelId(channelId);
            addition.getInputNodeConnectionByName(AdditionNode.INPUT_ARG_1).setChannelId(channelId);
            conveyor.addChannelId(channelId);
        }

        {
            String channelId  = Integer.toString(id++);
            inputB.getOutputNodeConnectionByName(StdInNumberInputNode.OUTPUT_RESULT).setChannelId(channelId);
            addition.getInputNodeConnectionByName(AdditionNode.INPUT_ARG_2).setChannelId(channelId);
            conveyor.addChannelId(channelId);
        }

        {
            String channelId  = Integer.toString(id++);
            addition.getOutputNodeConnectionByName(AdditionNode.OUTPUT_RESULT).setChannelId(channelId);
            outputC.getInputNodeConnectionByName(StdOutNumberOutputNode.INPUT_ARG).setChannelId(channelId);
            conveyor.addChannelId(channelId);
        }

        conveyor.addNodes(inputA, inputB, addition, outputC);

        try {
            Exporter.exportToFile("examples/math_test_1.iconv", conveyor);
        } catch (Exception ex) {

        }

        Engine engine = new SmartEngine(2);

        engine.addEngineStateObserver(new ConsoleLoggingEngineStateObserver());

        engine.addConveyorExecutionObserver(new ConsoleLoggingConveyorExecutionObserver());

        engine.setConveyor(conveyor);
        engine.start();
    }

}
